package com.ht.module.bus.controller;

import com.ht.module.bus.dto.RoomRuleChangeDto;
import com.ht.module.bus.dto.ScorePointsDto;
import com.ht.util.JsonResult;
import com.ht.util.Pager;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ht.module.bus.entity.BusRoom;
import com.ht.module.bus.service.BusRoomService;
import com.ht.util.*;

import javax.validation.Valid;
import java.math.BigDecimal;


/**
 * <p>
 * 房间表 前端控制器
 * </p>
 *
 * @author hejialun
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/bus-room")
public class BusRoomController {


    @Autowired
    private BusRoomService busRoomService;


    @ApiOperation("分页查询")
    @GetMapping("/findPage")
    public JsonResult findPage(Pager<BusRoom> pager, BusRoom en) {
        return JsonResult.success(busRoomService.page(pager));
    }


    @ApiOperation("通过id查询")
    @GetMapping("/get-by-id/{id}")
    public JsonResult getById(@PathVariable(value = "id") Integer id) {
        return JsonResult.success(busRoomService.findById(id));
    }

    @ApiOperation("创建房间")
    @PostMapping("/createRoom")
    public JsonResult createRoom(@RequestBody @Valid BusRoom en) {
        CommMethod.beanCreate(en);
        return JsonResult.success(busRoomService.createRoom(en));
    }


    @ApiOperation("通过id删除")
    @DeleteMapping("/delete-by-id/{id}")
    public JsonResult delete(@PathVariable(value = "id") String id) {
        busRoomService.removeById(id);
        return JsonResult.success();
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public JsonResult updateById(@RequestBody @Valid BusRoom en) {
        CommMethod.beanUpdate(en);
        busRoomService.updateById(en);
        return JsonResult.success();
    }

    @ApiOperation("解散房间")
    @DeleteMapping("/dissolveRoom/{roomId}")
    public JsonResult dissolveRoom(@PathVariable(value = "roomId") Integer roomId) {
        busRoomService.dissolveRoom(roomId);
        return JsonResult.success();
    }

    @ApiOperation("加入房间")
    @GetMapping("/joinRoom/{roomId}")
    public JsonResult joinRoom(@PathVariable(value = "roomId") Integer roomId) {
        busRoomService.joinRoom(roomId);
        return JsonResult.success();
    }

    @ApiOperation("退出房间")
    @PostMapping("/quitRoom/{roomId}")
    public JsonResult quitRoom(@PathVariable(value = "roomId") Integer roomId) {
        busRoomService.quitRoom(roomId);
        return JsonResult.success();
    }


    @ApiOperation("查询当前用户最近未开始的房间")
    @GetMapping("/findNoStartRoomByUser")
    public JsonResult findNoStartRoomByUser() {
        return JsonResult.success(busRoomService.findNoStartRoomByUser());
    }


    @ApiOperation("开始计分")
    @PostMapping("/startScoring")
    public JsonResult startScoring(@RequestBody @Valid RoomRuleChangeDto en) {
        busRoomService.startScoring(en);
        return JsonResult.success();
    }


    @ApiOperation("按时间查询出我加入的所有已经开始的房间")
    @GetMapping("/findStartRoomByTime")
    public JsonResult findStartRoomByTime(Pager<BusRoom> pager, BusRoom en) {
        return JsonResult.success(busRoomService.findStartRoomByTime(pager, en));
    }


    @ApiOperation("将用户踢出房间")
    @PostMapping("/kickOutRoom/{roomId}/{userId}")
    public JsonResult kickOutRoom(@PathVariable Integer roomId, @PathVariable String userId) {
        busRoomService.kickOutRoomById(roomId, userId);
        return JsonResult.success();
    }


    @ApiOperation("向积分池给分")
    @PostMapping("/givePoolScore/{roomId}/{score}")
    public JsonResult givePoolScore(@PathVariable Integer roomId, @PathVariable BigDecimal score) {
        busRoomService.givePoolScore(roomId, score);
        return JsonResult.success();
    }


    @ApiOperation("从积分池取分")
    @PostMapping("/takePoolScore/{roomId}/{score}")
    public JsonResult takePoolScore(@PathVariable Integer roomId, @PathVariable BigDecimal score) {
        busRoomService.takePoolScore(roomId, score);
        return JsonResult.success();
    }


    @ApiOperation("结束比赛")
    @PostMapping("/endGame/{roomId}")
    public JsonResult endGame(@PathVariable Integer roomId) {
        busRoomService.endGame(roomId);
        return JsonResult.success();
    }

    @ApiOperation("发送房间规则变化消息-通知房间内的人")
    @PostMapping("/roomRuleChangeMessage")
    public JsonResult roomRuleChangeMessage(@RequestBody @Valid RoomRuleChangeDto en) {
        busRoomService.roomRuleChangeMessage(en);
        return JsonResult.success();
    }

    @ApiOperation("给分")
    @PostMapping("/givePoints")
    public JsonResult givePoints(@RequestBody @Valid ScorePointsDto en) {
        busRoomService.givePoints(en);
        return JsonResult.success();
    }



    @ApiOperation("扣分-统一记模式，由房主扣分")
    @PostMapping("/deductScore")
    public JsonResult deductScore(@RequestBody @Valid ScorePointsDto en) {
        busRoomService.deductScore(en);
        return JsonResult.success();
    }



}
